package com.lp.mapreduce;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * 
 ************************************************************
 * @类名 : MyWCMapReduce2.java
 *
 * @DESCRIPTION : 单词统计MapReduce,优化
 * 1.加入combiner
 * 2.run的执行方式
 * @AUTHOR :  liaopin
 * @DATE :  2019年12月17日
 ************************************************************
 */
public class WCMapReduce2 extends Configured implements Tool {
    //Map
    public static class WCMap1 extends Mapper<LongWritable, Text, Text, IntWritable>{
        private static final IntWritable one = new IntWritable(1);
        private Text word = new Text();
        @Override
        protected void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {
            //value为一行数据,按分隔符拆分字符串
            String[] lineValues = value.toString().split(" ");
            for(String lineValue :lineValues){
                word.set(lineValue);
                //将拆分完成的单词写入上下文  key-value("test"-->1)
                context.write(word, one);
                //写入上下文后，map输出，加载到map task 内存中，进入shuffle阶段
            }
        }
    }
    //Reduce
    public static class WCReduce1 extends Reducer<Text, IntWritable, Text, IntWritable>{
        private IntWritable outputValue = new IntWritable();
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values,Context context)throws IOException, InterruptedException {
            int sum = 0;
            for(IntWritable value : values){
                sum += value.get();
            }
            outputValue.set(sum);
            context.write(key, outputValue);
        }
        
    }
    //Driver
    public int run(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
        //优化
        Configuration conf = this.getConf();
        Job job = Job.getInstance(conf, WCMapReduce2.class.getSimpleName());
        job.setJarByClass(WCMapReduce2.class);
        //## set job
        // input
        FileInputFormat.addInputPath(job, new Path(args[0]));
        //output
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        //Mapper
        job.setMapperClass(WCMap1.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //combiner
        //优化：每一个map完成后都单独执行一次reducer(不是所有的MapReduce都适合)
        job.setCombinerClass(WCReduce1.class);
        //Reducer
        job.setReducerClass(WCReduce1.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //submit job
        boolean isSuccess = job.waitForCompletion(true);
        return isSuccess ? 0 : 1;
    }
    
    //main
    public static void main(String[] args) throws Exception{
        Configuration conf = new Configuration();
        args = new String[]{"hdfs://bigdata-01.lp.com:8082/user/liaopin/input",
                            "hdfs://bigdata-01.lp.com:8082/user/liaopin/out4"};
        int  isSuccess = ToolRunner.run(conf, new WCMapReduce2(), args);
        System.exit(isSuccess);
    }

}
